package com.woniuxy.joylifeforum.utils;

import com.woniuxy.common.entity.ForumConcernExample;
import com.woniuxy.common.entity.ForumReplyExample;
import com.woniuxy.common.entity.ForumTopicExample;
import com.woniuxy.common.entity.ForumTopicPraiseExample;
import com.woniuxy.common.mapper.ForumConcernMapper;
import com.woniuxy.common.mapper.ForumReplyMapper;
import com.woniuxy.common.mapper.ForumTopicMapper;
import com.woniuxy.common.mapper.ForumTopicPraiseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * redis缓存
 */
@Service
public class ForumRedis {
    @Autowired
    private ForumConcernMapper forumConcernMapper;
    @Autowired
    private ForumTopicPraiseMapper forumTopicPraiseMapper;
    @Autowired
    private ForumReplyMapper forumReplyMapper;
    @Autowired
    private ForumTopicMapper forumTopicMapper;
    @Autowired
    private RedisUtil redisUtil;
    //查询社区成员数量
    public long findForumUserNum(String forumId){
        //查询redis中是否存在
        Object temp = redisUtil.get("forumNum"+forumId);
        if(temp!=null){
            //返回redis中信息
            return Long.parseLong(temp.toString());
        }
        //查询数据库
        ForumConcernExample concernExample=new ForumConcernExample();
        concernExample.createCriteria().andForumIdEqualTo(forumId);
        long forumNum = forumConcernMapper.countByExample(concernExample);
        //保存到缓存
        redisUtil.set("forumNum"+forumId, forumNum,600);
        return forumNum;
    }
    //查询社区话题数量
    public long findForumTopicNum(String forumId){
        //查询redis中是否存在
        Object temp = redisUtil.get("forumTopicNum"+forumId);
        if(temp!=null){
            //返回redis中信息
            return Long.parseLong(temp.toString());
        }
        //查询数据库
        ForumTopicExample topicExample=new ForumTopicExample();
        topicExample.createCriteria().andForumIdEqualTo(forumId);
        long topicNum = forumTopicMapper.countByExample(topicExample);
        //保存到缓存
        redisUtil.set("forumTopicNum"+forumId, topicNum,600);
        return topicNum;
    }
    //查询话题回复量
    public long findReplyNum(String topicId){
        //查询redis中是否存在
        Object temp = redisUtil.get("replyNum"+topicId);
        if(temp!=null){
            //返回redis中信息
            return Long.parseLong(temp.toString());
        }
        //查询数据库
        ForumReplyExample replyExample=new ForumReplyExample();
        ForumReplyExample.Criteria criteria = replyExample.createCriteria();
        //指定id
        criteria .andTopicIdEqualTo(topicId);
        //排除楼层回复
        criteria.andReplyIdIsNull();
        //状态正常
        criteria.andStatusEqualTo(0);
        long replyNum = forumReplyMapper.countByExample(replyExample);
        //保存到缓存
        redisUtil.set("replyNum"+topicId, replyNum,600);
        return replyNum;
    }
    //查询话题点赞量
    public long findTopicPraiseNum(String topicId){
        //查询redis中是否存在
        Object temp = redisUtil.get("topicPraiseNum"+topicId);
        if(temp!=null){
            //返回redis中信息
            return Long.parseLong(temp.toString());
        }
        //查询数据库
        ForumTopicPraiseExample praiseExample=new ForumTopicPraiseExample();
        praiseExample.createCriteria().andTopicIdEqualTo(topicId);
        long praiseNum = forumTopicPraiseMapper.countByExample(praiseExample);
        //保存到缓存
        redisUtil.set("topicPraiseNum"+topicId, praiseNum,600);
        return praiseNum;
    }
}
